package com.xindaoapp.happypet.viewpager.homegroup;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.activity.MyClubActivity;
import com.xindaoapp.happypet.bean.Groups.Group;
import com.xindaoapp.happypet.ui.FreeUseActivity;
import com.xindaoapp.happypet.view.CircleImageView;

public class PagerWindow {
    private final Context context;
    private final List<Group> groups;
    private View rootView;
    private RelativeLayout grone, grtwo, grthere, grfour;

    private CircleImageView img1, img2, img3, img4;
    private TextView name1, name2, name3, name4;

    public PagerWindow(Context context, List<Group> groups) {
        super();
        this.context = context;
        this.groups = groups;
        initViews();
    }

    private void initViews() {
        rootView = LayoutInflater.from(context).inflate(R.layout.home_groups, null);
        grone = (RelativeLayout) rootView.findViewById(R.id.grone);
        grtwo = (RelativeLayout) rootView.findViewById(R.id.grtwo);
        grthere = (RelativeLayout) rootView.findViewById(R.id.grthere);
        grfour = (RelativeLayout) rootView.findViewById(R.id.grfour);

        img1 = (CircleImageView) rootView.findViewById(R.id.img1);
        img2 = (CircleImageView) rootView.findViewById(R.id.img2);
        img3 = (CircleImageView) rootView.findViewById(R.id.img3);
        img4 = (CircleImageView) rootView.findViewById(R.id.img4);



        name1 = (TextView) rootView.findViewById(R.id.name1);
        name2 = (TextView) rootView.findViewById(R.id.name2);
        name3 = (TextView) rootView.findViewById(R.id.name3);
        name4 = (TextView) rootView.findViewById(R.id.name4);

        switch (groups.size()) {
            case 1:
                ImageLoader.getInstance().displayImage(groups.get(0).ico, img1);
                name1.setText(groups.get(0).name);
                oneClick();
                break;
            case 2:
                ImageLoader.getInstance().displayImage(groups.get(0).ico, img1);
                ImageLoader.getInstance().displayImage(groups.get(1).ico, img2);
                name1.setText(groups.get(0).name);
                name2.setText(groups.get(1).name);
                oneClick();
                twoClick();
                break;
            case 3:
                ImageLoader.getInstance().displayImage(groups.get(0).ico, img1);
                ImageLoader.getInstance().displayImage(groups.get(1).ico, img2);
                ImageLoader.getInstance().displayImage(groups.get(2).ico, img3);
                name1.setText(groups.get(0).name);
                name2.setText(groups.get(1).name);
                name3.setText(groups.get(2).name);
                oneClick();
                twoClick();
                thereClick();
                break;
            case 4:
                ImageLoader.getInstance().displayImage(groups.get(0).ico, img1);
                ImageLoader.getInstance().displayImage(groups.get(1).ico, img2);
                ImageLoader.getInstance().displayImage(groups.get(2).ico, img3);
                ImageLoader.getInstance().displayImage(groups.get(3).ico, img4);
                name1.setText(groups.get(0).name);
                name2.setText(groups.get(1).name);
                name3.setText(groups.get(2).name);
                name4.setText(groups.get(3).name);
                oneClick();
                twoClick();
                thereClick();
                fourClick();
                break;
            default:
                break;
        }

    }

    private void oneClick() {
        grone.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (groups.get(0).type.equals("shiyong")) {
                    Intent intent = new Intent(context, FreeUseActivity.class);
                    context.startActivity(intent);
                } else {

                    Intent intent = new Intent(context, MyClubActivity.class);
                    intent.putExtra("fId", groups.get(0).fid);
                    intent.putExtra("name", groups.get(0).name);
                    context.startActivity(intent);
                }
            }
        });
    }

    private void twoClick() {
        grtwo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (groups.get(1).type.equals("shiyong")) {
                    Intent intent = new Intent(context, FreeUseActivity.class);
                    context.startActivity(intent);
                } else {
                    Intent intent = new Intent(context, MyClubActivity.class);
                    intent.putExtra("fId", groups.get(1).fid);
                    intent.putExtra("name", groups.get(1).name);
                    context.startActivity(intent);
                }
            }
        });
    }

    private void thereClick() {
        grthere.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (groups.get(2).type.equals("shiyong")) {
                    Intent intent = new Intent(context, FreeUseActivity.class);
                    context.startActivity(intent);
                } else {
                    Intent intent = new Intent(context, MyClubActivity.class);
                    intent.putExtra("fId", groups.get(2).fid);
                    intent.putExtra("name", groups.get(2).name);
                    context.startActivity(intent);
                }
            }
        });
    }

    private void fourClick() {
        grfour.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (groups.get(3).type.equals("shiyong")) {
                    Intent intent = new Intent(context, FreeUseActivity.class);
                    context.startActivity(intent);
                } else {
                    Intent intent = new Intent(context, MyClubActivity.class);
                    intent.putExtra("fId", groups.get(3).fid);
                    intent.putExtra("name", groups.get(3).name);
                    context.startActivity(intent);
                }
            }
        });
    }

    public final synchronized View getView() {
        if (rootView == null) {
            initViews();
        }
        return rootView;
    }
}
